package com.example.ymm.modules1.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.bo.CacheMapService;
import com.example.ymm.model.CommonResult;
//import com.example.cloud_yuehong.modules1.feign.OrderFeignService;
//import com.example.cloud_yuehong.modules1.service.PdUserLoginService;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.entity.my.user.DbUserNote;
import com.example.ymm.modules1.service.dd.DdUserServer;
import com.example.ymm.modules1.service.dd.RobotGroupMessagesService;
import com.example.ymm.modules1.service.user.DbChatGroupService;
import com.example.ymm.modules1.service.user.DbDeptService;
import com.example.ymm.modules1.service.user.DbUserNoteService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/hl/test")
@Api(tags = "测试接口")
@Slf4j
public class Test {

//    @Autowired
//    PdUserLoginService pdUserLoginService;
    @Autowired
CacheMapService cacheMapService;

    @Autowired
    DdUserServer ddUserServer;
    @Autowired
    DbUserService dbUserService;

    @Autowired
    RobotGroupMessagesService robotGroupMessagesService;

    @Autowired
    DbChatGroupService dbChatGroupService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private DbDeptService dbDeptService;

    @Autowired
    private DbUserNoteService dbUserNoteService;



//    @Autowired
//    OrderFeignService ofs;

    @RequestMapping(value = "/testException", method = {RequestMethod.GET})
    @ApiOperation(value = "测试异常", notes = "测试异常")
    public CommonResult testException(
            HttpServletRequest request, HttpServletResponse response){
       // throw new YhException("未知异常");
        return CommonResult.failed("未知异常");
    }

    @RequestMapping(value = "/testFn", method = {RequestMethod.GET})
    @ApiOperation(value = "查询用户信息", notes = "测试标题")
    public CommonResult testFn(
            String openConversationId,
            HttpServletRequest request, HttpServletResponse response){
        try {
            dbChatGroupService.sendGroupListMessage();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(), e);
           return CommonResult.error(e.getMessage());
        }

    }



    @RequestMapping(value = "/testOneFn", method = {RequestMethod.GET})
    @ApiOperation(value = "发送一个", notes = "测试标题")
    public CommonResult testOneFn(
            String openConversationId,
            HttpServletRequest request, HttpServletResponse response){
        try {
            List<DbUser> users = dbChatGroupService.testSend(openConversationId);
            return CommonResult.success(users);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/getDeptUserList", method = {RequestMethod.GET})
    @ApiOperation(value = "查询部门用户信息", notes = "测试标题")
    public CommonResult getDeptUserList(
            HttpServletRequest request, HttpServletResponse response){
        try {
            JSONObject json = ddUserServer.getDeptUserList();
            return CommonResult.success(json);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/syncDeptList", method = {RequestMethod.GET})
    @ApiOperation(value = "同步部门信息", notes = "测试标题")
    public CommonResult syncDeptList(
            HttpServletRequest request, HttpServletResponse response){
        try {
            dbDeptService.syncUpLoad();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @RequestMapping(value = "/testLogin", method = {RequestMethod.GET})
    @ApiOperation(value = "测试登录", notes = "测试登录")
    public CommonResult testLogin(
            String userId,
            HttpServletRequest request, HttpServletResponse response){
        try {
            return dbUserService.loginByUserId(userId);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }




    @RequestMapping(value = "/testGroupQueryFn", method = {RequestMethod.GET})
    @ApiOperation(value = "同步离职人员", notes = "查询列表2")
    public CommonResult testQueryFn(
            HttpServletRequest request, HttpServletResponse response){
        try{
            dbUserService.sycnDimissionUserList();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return null;
        }
    }


    @RequestMapping(value = "/testMp3One", method = {RequestMethod.GET})
    @ApiOperation(value = "同步一条", notes = "查询列表2")
    public CommonResult testMp3One(
            String noteId,
            HttpServletRequest request, HttpServletResponse response){
        try{
            DbUserNote note = dbUserNoteService.getById(noteId);

            String s = dbUserNoteService.syncUpUserNoteByOne(note);
            note.setMp3Url(s);
            dbUserNoteService.saveOrUpdate(note);
            return CommonResult.success(s,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return null;
        }
    }

//    @RequestMapping(value = "/testGetUserNote", method = {RequestMethod.GET})
//    @ApiOperation(value = "同步所有mp3", notes = "测试标题")
//    public CommonResult testGetUserNote(
//            HttpServletRequest request, HttpServletResponse response){
//        try {
//            List<DbCard> dbUserNotes = dbUserNoteService.testGetUserNote();
//            return CommonResult.success(dbUserNotes);
//        }catch (Exception e){
//            log.error(e.getMessage(), e);
//            return CommonResult.error(e.getMessage());
//        }
//
//    }


//    @RequestMapping(value = "/testRpcFn", method = {RequestMethod.GET})
//    @ApiOperation(value = "服务之间接口调用", notes = "服务之间接口调用")
//    public R testRpcFn(
//            HttpServletRequest request, HttpServletResponse response){
//        try {
//            return ofs.findOrderByUserId(1);
//        }catch (Exception e){
//            log.error(e.getMessage(), e);
//            return R.error(e.getMessage());
//        }
//    }

//    @RequestMapping(value = "/testSave", method = {RequestMethod.GET})
//    @ApiOperation(value = "保存1", notes = "保存1")
//    public CommonResult testSave(
//            PdUserLogin pdUserLogin,
//            HttpServletRequest request, HttpServletResponse response){
//        try{
//            boolean save = pdUserLoginService.save(pdUserLogin);
//            if(save){
//                return CommonResult.success();
//            }else{
//                return CommonResult.failed("保存失败");
//            }
//        }catch (Exception e){
//            log.error(e.getMessage(), e);
//            return CommonResult.failed("保存失败"+e.getMessage());
//        }
//    }

    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

    /**
     * 测试限流注解，下面配置说明该接口 60秒内最多只能访问 10次，保存到redis的键名为 limit_test，
     */
    @RequestMapping(value = "/testLimit", method = {RequestMethod.GET})
    @ApiOperation("测试")
    public int testLimit() {
        return ATOMIC_INTEGER.incrementAndGet();
    }


}
